﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Data;
using WisDom.SmartTMS.Dtos;
using WisDom.SmartTMS.Entities;
using WisDom.SmartTMS.Interface;

namespace WisDom.SmartTMS.Service
{
    [RemoteService(IsEnabled = true)]//设置flse则不自动生成API
    public class ShipperService : ApplicationService,IShipperService
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="departmentRepository"></param>
        private readonly IBusinessAuditRepository _auditRepository;
        private readonly IRepository<Shipper, Guid> _irepository;
        private readonly IRepository<BusinessAuditLogs, Guid> _irepository1;
        private readonly IShipperRepository _shippers;
        public ShipperService(IShipperRepository shippers, IBusinessAuditRepository auditRepository, IRepository<Shipper, Guid> irepository, IRepository<BusinessAuditLogs, Guid> irepository1)
        {
            _auditRepository = auditRepository;
            _irepository = irepository;
            _shippers = shippers;
            _irepository1 = irepository1;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ShipperAuditCreateDto> CreateAsync([FromBody]ShipperAuditCreateDto input)
        {
            Shipper shipper = new(
                GuidGenerator.Create(),
                input.ShipperName,
                input.MobilePhoto,
                input.CompanyName,
                input.Address,
                input.LicenseValidity,
                input.Remark,
                input.LlicencePhoto,
                GuidGenerator.Create()
               
               );


            var result = await _irepository.InsertAsync(shipper);


            BusinessAudit b = new(
                GuidGenerator.Create(),
                result.Id,
                Enums.BusinessType.Shipper,
                DateTime.Now,
                GuidGenerator.Create(),
                result.Address
                );
            _ = _auditRepository.InsertAsync(b);

            BusinessAuditLogs c = new(
                GuidGenerator.Create(),
                GuidGenerator.Create(),
                GuidGenerator.Create(),
                "admin",
                Enums.AuditState.InAudit
                );
            _ = _irepository1.InsertAsync(c);
            return ObjectMapper.Map<Shipper, ShipperAuditCreateDto>(result);
        }


        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        [HttpGet] 
        public async Task<PagedResultDto<ShipperDto>> GetShipperAsync(PagedAndSortedResultRequestDto input)
        {
            //创建一个查询对象
            var query = _irepository.OrderBy(input.Sorting);
            //总条数
            var totalCount = await query.CountAsync();

            //从仓储获取实体数据对象
            List<Shipper> leaveDeal = await query
                .Skip(input.SkipCount)
                .Take(input.MaxResultCount).ToListAsync();

            //进行实体转换Dto
            List<ShipperDto> leaveDealDto =
                ObjectMapper.Map<List<Shipper>, List<ShipperDto>>(leaveDeal);
            //返回结果
            return new PagedResultDto<ShipperDto>(totalCount, leaveDealDto);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteShipper(Guid id)
        {
            await _irepository.DeleteAsync(id);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ShipperDto> PayFill(Guid id)
        {
            var a = await _irepository.GetAsync(id);
            return ObjectMapper.Map<Shipper, ShipperDto>(a);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="pay"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ShipperDto> UptShipper([FromBody] ShipperDto input)
        {

            Shipper shipper = new(
                 input.Id,
                 //GuidGenerator.Create(),
                input.ShipperName,
                input.MobilePhoto,
                input.CompanyName,
                input.Address,
                input.LicenseValidity,
                input.Remark,
                input.LlicencePhoto,
                GuidGenerator.Create()

               );

            var result = await _irepository.UpdateAsync(shipper);
            return ObjectMapper.Map<Shipper, ShipperDto>(result);
        }
    }
}
